package com.ddtech.business.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ddtech.business.common.BaseService;
import com.ddtech.business.constant.Del;
import com.ddtech.business.train.entity.TrainGrade;
import com.ddtech.business.train.mapper.TrainGradeMapper;
import com.ddtech.business.train.query.TrainGradeQuery;
import com.ddtech.business.train.service.TrainGradeService;
import com.ddtech.config.DataScope;
import com.ddtech.framework.system.service.DeptService;
import com.ddtech.framework.system.service.UserService;
import com.ddtech.framework.system.service.dto.UserDTO;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import com.ddtech.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @version v1.0
 * @ClassName:TrainGradeServiceImpl description：
 * @author:wei_zhang(xiao)
 * @date: 2023-12-26 20:45
 */
@Service
public class TrainGradeServiceImpl extends BaseService<TrainGradeMapper, TrainGrade> implements TrainGradeService {

    @Resource
    private TrainGradeMapper trainGradeMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private DataScope dataScope;

    @Autowired
    private DeptService deptService;

    @Override
    public void create(TrainGrade trainGrade) {
        trainGrade.init();
        UserDTO userDTO = userService.findById(trainGrade.getUserId());
        if (userDTO!=null) {
            trainGrade.setUserName(userDTO.getName());
            trainGrade.setUserAccount(userDTO.getUsername());
        }
        this.saveOrUpdate(trainGrade);
    }

    /**
     * description: 获取用户训练成绩
     * @param:  * @param account
     * @date: 2023-12-26
     * @return: com.ddtech.business.train.entity.TrainGrade
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object bindUser(String account) {
        return trainGradeMapper.bindUser(account);
    }

    @Override
    public Object findGrade(TrainGradeQuery trainGradeQuery) {
         LambdaQueryWrapper<TrainGrade> recordLambdaQueryWrapper = Wrappers.lambdaQuery();
        recordLambdaQueryWrapper.eq(TrainGrade::getUserId, trainGradeQuery.getUserId());
        recordLambdaQueryWrapper.orderByDesc(TrainGrade::getCreateTime);
        //List<TrainGrade> ls = this.list(recordLambdaQueryWrapper);
        return this.list(recordLambdaQueryWrapper);
    }

    @Override
    public Object findVrGrade(TrainGradeQuery trainGradeQuery) {
        LambdaQueryWrapper<TrainGrade> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotEmpty(trainGradeQuery.getUserName()) ){
            lambdaQueryWrapper.like(TrainGrade::getUserName, trainGradeQuery.getUserName());
        }
        if(StringUtils.isNotEmpty(trainGradeQuery.getDeptId())){
            Set<String> deptIds=this.queryDeptIds(trainGradeQuery.getDeptId());
            if(deptIds.size()>0){
                lambdaQueryWrapper.in(TrainGrade::getDeptId, deptIds);
            }
        }
        if(StringUtils.isNotBlank(trainGradeQuery.getStartTime())){
            lambdaQueryWrapper.ge(TrainGrade::getCreateTime,trainGradeQuery.getStartTime()+" 00:00:00");
        }
        if(StringUtils.isNotBlank(trainGradeQuery.getEndTime())){
            lambdaQueryWrapper.le(TrainGrade::getCreateTime,trainGradeQuery.getEndTime() +" 23:59:59");
        }
        lambdaQueryWrapper.orderByDesc(TrainGrade::getCreateTime);
        IPage<TrainGrade> page = this.page(trainGradeQuery.populatePage(), lambdaQueryWrapper);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object findVrUserGrade(IPage pageableRequest, String startTime, String endTime) {
        LambdaQueryWrapper<TrainGrade> recordLambdaQueryWrapper = Wrappers.lambdaQuery();
        recordLambdaQueryWrapper.eq(TrainGrade::getUserId, SecurityUtils.getUserId());
        recordLambdaQueryWrapper.eq(TrainGrade::getIsDelete, Del.FALSE.getStatus());
        recordLambdaQueryWrapper.orderByDesc(TrainGrade::getCreateTime);
        if(StringUtils.isNotBlank(startTime)){
            recordLambdaQueryWrapper.ge(TrainGrade::getCreateTime,startTime +" 00:00:00");
        }
        if(StringUtils.isNotBlank(endTime)){
            recordLambdaQueryWrapper.le(TrainGrade::getCreateTime,endTime +" 23:59:59");
        }
        recordLambdaQueryWrapper.orderByDesc(TrainGrade::getCreateTime);
        IPage page = this.page(pageableRequest,recordLambdaQueryWrapper);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Set<String> queryDeptIds(String deptId) {
        Set<String> deptSet = new HashSet<>();
        deptSet.add(deptId);
        deptSet.addAll(dataScope.getDeptChildren(deptService.findByPid(deptId)));
        return deptSet;
    }
}
